package nc.impl.dm.m4804.rule;

import java.util.HashMap;
import java.util.Map;
import nc.bs.framework.common.NCLocator;
import nc.bs.ml.NCLangResOnserver;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.pubitf.scmf.dm.vehicle.IVehicleQueryService;
import nc.pubitf.scmf.dm.vehicletype.IVehicleTypeQueryService;
import nc.vo.dm.m4804.entity.DelivBillAggVO;
import nc.vo.dm.m4804.entity.DelivBillBVO;
import nc.vo.dm.m4804.entity.DelivBillPackVO;
import nc.vo.dm.m4804trantype.entity.M4804TranTypeVO;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.MathTool;
import nc.vo.pubapp.pattern.pub.PubAppTool;
import nc.vo.scmf.dm.vehicle.entity.VehicleVO;
import nc.vo.scmf.dm.vehicletype.entity.VehicleTypeVO;











public class BillWeightCheck implements IRule<DelivBillAggVO> {
  private Map<String, M4804TranTypeVO> tranTypeMap = new HashMap<>();



  private Map<String, UFDouble> vehicleIndex = new HashMap<>();





  private Map<String, UFDouble> vehicleTypeIndex = new HashMap<>();



private Object vehicleQueryService;



private Object vehicleTypeQueryService;






  public BillWeightCheck(Map<String, M4804TranTypeVO> tranTypeMap) {
    this.tranTypeMap = tranTypeMap;
  }

  public IVehicleQueryService getVehicleQueryService() {
    if (this.vehicleQueryService == null)
      this
        .vehicleQueryService = (IVehicleQueryService)NCLocator.getInstance().lookup(IVehicleQueryService.class); 

    return (IVehicleQueryService) this.vehicleQueryService;
  }

  public IVehicleTypeQueryService getVehicleTypeQueryService() {
    if (this.vehicleTypeQueryService == null)
      this
        .vehicleTypeQueryService = (IVehicleTypeQueryService)NCLocator.getInstance().lookup(IVehicleTypeQueryService.class); 

    return (IVehicleTypeQueryService) this.vehicleTypeQueryService;
  }


  public void process(DelivBillAggVO[] vos) {
    for (DelivBillAggVO bill : vos)
      processBillWeightCheck(bill); 
  }








  private void checkBillBody(DelivBillAggVO bill, UFDouble max) {
    DelivBillBVO[] vos = bill.getDelivBillBVO();
    if (vos == null || vos.length == 0)

      return; 
    for (DelivBillBVO vo : vos) {
      if (vo.getStatus() != 3) {


        UFDouble nweight = vo.getNweight();
        UFDouble uFDouble1 = MathTool.sub(max, nweight);
        if (uFDouble1.doubleValue() < 0.0D) {

          String message = NCLangResOnserver.getInstance().getStrByID("4014001_0", "04014001-0048", null, new String[] { nweight
                
                .toString(), max.toString() });

          ExceptionUtils.wrappBusinessException(message);
        } 
      } 
    } 
    UFDouble ntotalweight = bill.getParentVO().getNtotalweight();
    UFDouble diff = MathTool.sub(max, ntotalweight);
    if (diff.doubleValue() < 0.0D) {

      String message = NCLangResOnserver.getInstance().getStrByID("4014001_0", "04014001-0049", null, new String[] { ntotalweight
            
            .toString(), max.toString() });

      ExceptionUtils.wrappBusinessException(message);
    } 
  }

  private void checkPackBody(DelivBillAggVO bill, UFDouble max) {
    DelivBillPackVO[] vos = bill.getDelivBillPackVO();
    if (vos == null || vos.length == 0)

      return; 
    for (DelivBillPackVO vo : vos) {
      if (vo.getStatus() != 3) {


        UFDouble npackweight = vo.getNpackweight();
        UFDouble uFDouble1 = MathTool.sub(max, npackweight);
        if (uFDouble1.doubleValue() < 0.0D) {

          String message = NCLangResOnserver.getInstance().getStrByID("4014001_0", "04014001-0050", null, new String[] { npackweight
                
                .toString(), max.toString() });

          ExceptionUtils.wrappBusinessException(message);
        } 
      } 
    } 
    UFDouble ntotalpackweight = bill.getParentVO().getNtotalpackweight();
    UFDouble diff = MathTool.sub(max, ntotalpackweight);
    if (diff.doubleValue() < 0.0D) {

      String message = NCLangResOnserver.getInstance().getStrByID("4014001_0", "04014001-0051", null, new String[] { ntotalpackweight
            
            .toString(), max.toString() });

      ExceptionUtils.wrappBusinessException(message);
    } 
  }







  private UFDouble getMaxWeight(DelivBillAggVO bill) {
    UFDouble max = null;
    String cvehicleid = bill.getParentVO().getCvehicleid();
    String cvehicletypeid = bill.getParentVO().getCvehicletypeid();
    if (cvehicleid != null) {
      max = loadVehicleWight(cvehicleid);

    } else if (cvehicletypeid != null) {
      max = loadVehicleTypeWight(cvehicletypeid);
    } 
    return max;
  }







  private UFDouble loadVehicleTypeWight(String cvehicletypeid) {
    UFDouble weight = null;
    try {
      if (this.vehicleTypeIndex.containsKey(cvehicletypeid))
        return this.vehicleTypeIndex.get(cvehicletypeid); 


      VehicleTypeVO vehicleTypeVO = getVehicleTypeQueryService().getVehicleType(cvehicletypeid);
      if (vehicleTypeVO != null) {
        weight = vehicleTypeVO.getNload();
        this.vehicleTypeIndex.put(cvehicletypeid, weight);

      } 
    } catch (Exception e) {
      ExceptionUtils.wrappException(e);
    } 
    return weight;
  }







  private UFDouble loadVehicleWight(String cvehicleid) {
    UFDouble weight = null;
    try {
      if (this.vehicleIndex.containsKey(cvehicleid))
        return this.vehicleIndex.get(cvehicleid); 


      VehicleVO vehicleVO = getVehicleQueryService().getVehicleVo(cvehicleid);
      if (vehicleVO != null) {
        weight = vehicleVO.getNload();
        this.vehicleIndex.put(cvehicleid, weight);

      } 
    } catch (Exception e) {
      ExceptionUtils.wrappException(e);
    } 
    return weight;
  }

  private void processBillWeightCheck(DelivBillAggVO bill) {
    try {
      String tranTypeCode = bill.getParentVO().getVtrantypecode();
      if (PubAppTool.isNull(tranTypeCode))


        return; 
      M4804TranTypeVO typeVO = this.tranTypeMap.get(tranTypeCode);




      boolean noCheck = (typeVO == null || typeVO.getBcheckweightflag() == null || !typeVO.getBcheckweightflag().booleanValue());
      if (noCheck)


        return; 
      UFDouble max = getMaxWeight(bill);
      if (max == null)

        return; 
      checkBillBody(bill, max);
      checkPackBody(bill, max);

    } catch (Exception e) {
      ExceptionUtils.wrappException(e);
    } 
  }
}

